<html>

<head>
    <!--
     Licensed to the Apache Software Foundation (ASF) under one or more
     contributor license agreements.  See the NOTICE file distributed with
     this work for additional information regarding copyright ownership.
     The ASF licenses this file to You under the Apache License, Version 2.0
     (the "License"); you may not use this file except in compliance with
     the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
    -->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Storm UI</title>
    <link href="/css/bootstrap-3.3.1.min.css" rel="stylesheet" type="text/css">
    <link href="/css/jquery.dataTables.1.10.4.min.css" rel="stylesheet" type="text/css">
    <link href="/css/dataTables.bootstrap.css" rel="stylesheet" type="text/css">
    <link href="/css/jsonFormatter.min.css" rel="stylesheet" type="text/css">
    <link href="/css/style.css?_ts=${packageTimestamp}" rel="stylesheet" type="text/css">
    <script src="/js/jquery-3.5.1.min.js" type="text/javascript"></script>
    <script src="/js/jquery.dataTables.1.10.4.min.js" type="text/javascript"></script>
    <script src="/js/jquery.cookies.2.2.0.min.js" type="text/javascript"></script>
    <script src="/js/jquery.mustache.js" type="text/javascript"></script>
    <script src="/js/url.min.js" type="text/javascript"></script>
    <script src="/js/bootstrap-3.3.1.min.js" type="text/javascript"></script>
    <script src="/js/jquery.blockUI.min.js" type="text/javascript"></script>
    <script src="/js/jsonFormatter.min.js" type="text/javascript"></script>
    <script src="/js/script.js?_ts=${packageTimestamp}" type="text/javascript"></script>
</head>

<body>
  <div class="warning" id="ras-warning-fixed-buffer">
    <H1>This user's topologies are in danger of being unscheduled due to the owner's over-use of cluster resources.</H1>
    <p>Please keep this user's resource consumption within guaranteed bounds to ensure topologies for this user will continue to run.</p>
  </div>
  <div class="warning" id="ras-warning-top-buffer"></div>
  <div class="container-fluid">
    <div class="row">
        <div class="col-md-11">
            <h1><a href="/">Storm UI</a></h1>
        </div>
        <div id="ui-user" class="col-md-1"></div>
    </div>
    <div class="row">
        <div class="col-md-12" id="search-form">
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <h2>Owner Summary</h2>
            <div id="owner-summary"></div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <h2 id="owner-resource-usage-summary-header">Owner Resource Usage</h2>
            <div id="owner-resource-usage-summary"></div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <h2 id="owner-resource-guarantee-summary-header">Owner Resource Guarantees (RAS Topologies Only)</h2>
            <div id="owner-resource-guarantee-summary"></div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <h2>Owner Topologies</h2>
            <div id="topology-summary"></div>
        </div>
    </div>
    <div class="row">
        <div id="json-response-error" class="col-md-12"></div>
    </div>
    <div>
        <p id="page-rendered-at-timestamp"></p>
    </div>
</div>
</body>
<script>
    $(document).ajaxStop($.unblockUI);
    $(document).ajaxStart(function() {
        $.blockUI({
            message: '<img src="images/spinner.gif" /> <h3>Loading summary...</h3>'
        });
    });
    $(document).ready(function() {

        var owner = $.url("?id");
        if (!owner) {
            // this page is designed to be per owner, handle the case where the URL is malformed
            getStatic("/templates/json-error-template.html", function(template) {
                $("#json-response-error").append(Mustache.render($(template).filter("#json-error-template").html(), {
                    "error": "Invalid owner",
                    "errorMessage": "Please pass an owner id with the id URL parameter"
                }));
            });
            return;
        }
        var url = "/api/v1/owner-resources/" + owner;

        $.extend($.fn.dataTable.defaults, {
            stateSave: true,
            lengthMenu: [
                [20, 40, 60, 100, -1],
                [20, 40, 60, 100, "All"]
            ],
            pageLength: 20
        });

        $.ajaxSetup({
            "error": function(jqXHR, textStatus, response) {
                var errorJson = jQuery.parseJSON(jqXHR.responseText);
                getStatic("/templates/json-error-template.html", function(template) {
                    $("#json-response-error").append(Mustache.render($(template).filter("#json-error-template").html(), errorJson));
                });
            }
        });

        function jsError(other) {
            try {
                other();
            } catch (err) {
                getStatic("/templates/json-error-template.html", function(template) {
                    $("#json-response-error").append(Mustache.render($(template).filter("#json-error-template").html(), {
                        error: "JS Error",
                        errorMessage: err
                    }));
                });
            }
        }

        var topologySummary = $("#topology-summary");
        var ownerResourceUsage = $("#owner-resource-usage-summary");
        var ownerSummary = $("#owner-summary");
        var ownerResourceGuarantee = $("#owner-resource-guarantee-summary");
        $.getJSON(url, function(response, status, jqXHR) {
            getStatic("/templates/owner-page-template.html", function(template) {
                jsError(function() {
                    //owner,totalTopologies,totalTasks,totalExecutors,totalWorkers
                    ownerSummary.append(
                        Mustache.render($(template).filter("#owner-summary-template").html(), response));

                    //totalReqOnHeapMem,totalReqOffHeapMem,totalReqMem,totalReqCpu,totalAssignedOnHeapMem,totalAssignedOffHeapMem,totalAssignedMem,totalAssignedCpu
                    ownerResourceUsage.append(
                        Mustache.render($(template).filter("#owner-resource-usage-template").html(), response));
                    $('#owner-resource-usage-summary [data-toggle="tooltip"]').tooltip();

                    if (response["cpuGuarantee"] != "N/A" || response["memoryGuarantee"] != "N/A") {
                        ownerResourceGuarantee.append(
                            Mustache.render($(template).filter("#owner-resource-guarantee-template").html(), response));
                        $('#owner-resource-guarantee-summary [data-toggle="tooltip"]').tooltip();

                        $('#mem-guarantee-util').html(getResourceGuaranteeRemainingFormat("display", response["memoryGuaranteeRemaining"]));

                        $('#cpu-guarantee-util').html(getResourceGuaranteeRemainingFormat("display", response["cpuGuaranteeRemaining"]));

                    } else {
                        $('#owner-resource-guarantee-summary-header').hide();
                        $('#owner-resource-guarantee-summary').hide();
                    }

                    var displayResource = response["schedulerDisplayResource"];
                    if (!displayResource) {
                        $('#owner-resource-usage-summary-header').hide();
                        $('#owner-resource-usage-summary').hide();
                        $('#owner-resource-guarantee-summary-header').hide();
                        $('#owner-resource-guarantee-summary').hide();
                    }

                    if(response && (response["memoryGuaranteeRemaining"] < 0 || response["cpuGuaranteeRemaining"] < 0
                        || response["memoryGuaranteeRemaining"] == "N/A" || response["cpuGuaranteeRemaining"] == "N/A")) {
                        $(".warning").show();
                    } else {
                        $(".warning").hide();
                    }

                    $('#owner-resource-usage-summary [data-toggle="tooltip"]').tooltip();

                    topologySummary.append(
                        Mustache.render($(template).filter("#owner-topology-summary-template").html(), response));
                    if (displayResource) {
                        // (0) name, (1) owner, (2) status, (3) uptime, (4) workersTotal, (5) executorsTotal, (6) tasksTotal,
                        // (7) assignedTotalMem, [ (8) assignedCpu, ] (9), schedulerInfo, (10) topologyVersion, (11) stormVersion
                        dtAutoPage("#owner-topology-summary-table", {
                            columnDefs: [{
                                type: "num", targets: [4, 5, 6]
                            }, {
                                type: "num", targets: [7, 8], render: $.fn.dataTable.render.number(',', '.', 0)
                            }, {
                                type: "time-str", targets: [3]
                            }]
                        });
                    } else {
                        // (0) name, (1) owner, (2) status, (3) uptime, (4) workersTotal, (5) executorsTotal, (6) tasksTotal,
                        // (7) assignedTotalMem, (8), schedulerInfo, (9) topologyVersion, (10) stormVersion
                        dtAutoPage("#owner-topology-summary-table", {
                            columnDefs: [{
                                type: "num", targets: [4, 5, 6]
                            }, {
                                type: "num", targets: [7], render: $.fn.dataTable.render.number(',', '.', 0)
                            }, {
                                type: "time-str", targets: [3]
                            }]
                        });
                    }
                    $('#topology-summary [data-toggle="tooltip"]').tooltip();

                });
            });
        });
    });

    getPageRenderedTimestamp("page-rendered-at-timestamp");

</script>

</html>
